home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / speech / examples / recognize.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.8 KB  |  197 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include    <stream.h>
  18. #include    <assert.h>
  19. #include    <speech/CharString.h>
  20. #include    <speech/Recognizer.h>
  21. #include    <speech/Word.h>
  22. #include    <speech/WordCallbackBindings.h>
  23. #include    <speech/Vocabulary.h>
  24. #include    <speech/Condition.h>
  25. #include    <speech/Event.h>
  26. #include    <speech/Error.h>
  27. #include    <speech/Shorthand.h>
  28.  
  29. void doThis( const Event* event, void* /*v*/ )
  30.     { cout << "do this called with " << event->type() << "\n" << flush ; }
  31. void doThat( const Event* event, void* /*v*/ )
  32.     { cout << "do that called with " << event->type() << "\n" << flush ; }
  33. void doTheOther( const Event* event, void* /*v*/ )
  34.     { cout << "do the other called with " << event->type() << "\n" << flush ; }
  35.  
  36. void usage( const char* const /*progName*/ )
  37.     { cerr << "progName <word> <word2>\n" << flush ; exit( -1 ) ; }
  38.  
  39. int main( int argc, char* argv[] )
  40. {
  41.     char* wordName1 ;
  42.     char* wordName2 ;
  43.     char* wordName3 ;
  44.  
  45.     char* applicationClass = "Recognize" ;
  46.     char* applicationName = argv[0] ;
  47.  
  48.     XrmDatabase xrmDatabase        // toolkit-dependent
  49.             = XrmGetFileDatabase( CharString( getenv( "HOME" ) ) + "/.Xdefaults" ) ;
  50.  
  51.     Recognizer recognizer( applicationClass, applicationName, xrmDatabase,
  52.             "",     // default display, program name
  53.             Recognizer::RecognitionInterest     // get recognition events and all others
  54.                     | Recognizer::RejectionInterest
  55.                     | Recognizer::AmbiguousInterest
  56.                     | Recognizer::ErrorInterest ) ;
  57.     assert( recognizer.status( ) == Error::OK ) ;
  58.  
  59.     unsigned char swapConditionsUsingVocabularies = 0 ;
  60.  
  61.     for( long i = 1 ; i < argc ; i++ )
  62.         if( argv[i][0] == '-' )
  63.             switch( argv[i][1] )
  64.                 {
  65.                 case 's': i++ ; swapConditionsUsingVocabularies=1 ; break ;
  66.                 case 'v': i++ ; /* ;*/ break ;
  67.                 case 'd': i++ ; /* ;*/ break ;
  68.                 default: usage( argv[0] ) ; break ;
  69.                 }
  70.          else
  71.             break ;
  72.  
  73.     wordName1 = "doThis" ;
  74.     wordName2 = "doThat" ;
  75.     wordName3 = "doTheOther" ;
  76.  
  77.     if( i < argc )
  78.         wordName1 = argv[i++] ;
  79.     if( i < argc )
  80.         wordName2 = argv[i++] ;
  81.     if( i < argc )
  82.         wordName3 = argv[i++] ;
  83.  
  84.  
  85.     // words with global focus
  86.  
  87. #ifdef OLD
  88.     ActionFunctionBinding globalActionFunctionBindings[] = {
  89.         { "doThis", doThis, (void*)0 },
  90.         { "doThat", doThat, (void*)0 },
  91.         { (char*)0, (CallbackFunctionPointer)0, (void*)0 },
  92.  
  93.     globalActionFunctionBindings[0].actionNameValue = wordName1 ;
  94.     globalActionFunctionBindings[1].actionNameValue = wordName2 ;
  95.         } ;
  96. #else
  97.     ActionFunctionBinding globalActionFunctionBindings[] = {
  98.         ActionFunctionBinding( "doThis", doThis, (void*)0 ),
  99.         ActionFunctionBinding( "doThat", doThat, (void*)0 ),
  100.         ActionFunctionBinding( (char*)0, (CallbackFunctionPointer)0, (void*)0 ),
  101.         } ;
  102. #endif
  103.  
  104.     Vocabulary* globalWords ;
  105.     recognizer.newWords( globalActionFunctionBindings,
  106.             Condition( Condition::Global ),
  107.             globalWords ) ;
  108.     if( recognizer.status() != Error::OK )
  109.         cerr << "trouble loading global words\n" << flush ;
  110.  
  111.  
  112.     // words with window focus
  113.  
  114. #ifdef OLD
  115.     ActionFunctionBinding localActionFunctionBindings[] = {
  116.         { "doTheOther", doTheOther, (void*)0 },
  117.         { (char*)0, (CallbackFunctionPointer)0, (void*)0 },
  118.         } ;
  119.  
  120.     localActionFunctionBindings[0].actionNameValue = wordName3 ;
  121. #else
  122.     ActionFunctionBinding localActionFunctionBindings[] = {
  123.         ActionFunctionBinding( "doTheOther", doTheOther, (void*)0 ),
  124.         ActionFunctionBinding( (char*)0, (CallbackFunctionPointer)0, (void*)0 ),
  125.         } ;
  126. #endif
  127.  
  128.     // toolkit dependent
  129.     void* v = "client data" ;
  130.     int screen = DefaultScreen( recognizer.dpy() ) ;
  131.     Window root = RootWindow( recognizer.dpy(), screen ) ;
  132.     long white = WhitePixel( recognizer.dpy(), screen ) ;
  133.     long black = BlackPixel( recognizer.dpy(), screen ) ;
  134.     Window window = XCreateSimpleWindow( recognizer.dpy(), root,
  135.             100, 100, 100, 100, 1, white, black ) ;
  136.     XMapWindow( recognizer.dpy(), window ) ;
  137.  
  138.     Vocabulary* localWords ;
  139.     recognizer.newWords( localActionFunctionBindings,
  140.             Condition( window ),
  141.             localWords ) ;
  142.     if( recognizer.status() != Error::OK )
  143.         cerr << "trouble loading local words\n" << flush ;
  144.  
  145. #ifdef TODO
  146.     gw.enable( win ) ;
  147.     lw.enable( Global ) ;
  148. #endif
  149.  
  150.     // vocabulary usage
  151.     if( swapConditionsUsingVocabularies )
  152.         {
  153.         cerr << "swapping conditions using vocabularies\n" << flush ;
  154.  
  155.         {
  156.         Vocabulary vocabulary( recognizer ) ;
  157.         Word* word ;
  158.         for( long i = globalWords->first() ;
  159.                 globalWords->get( i, word ) ;
  160.                 i = globalWords->next( i ) )
  161.             vocabulary.add( *word ) ; 
  162.         Condition localCondition( window ) ;
  163.         vocabulary.enable( localCondition ) ;
  164.         }
  165.  
  166.         {
  167.         Vocabulary vocabulary( recognizer ) ;
  168.         Word* word ;
  169.         for( long i = localWords->first() ;
  170.                 localWords->get( i, word ) ;
  171.                 i = localWords->next( i ) )
  172.             vocabulary.add( *word ) ; 
  173.         Condition globalCondition( Condition::Global ) ;
  174.         vocabulary.enable( globalCondition ) ;
  175.         }
  176.         }
  177.  
  178.     if( globalWords ) delete globalWords ;
  179.     if( localWords ) delete localWords ;
  180.  
  181.     // toolkit-dependent modification here
  182.     XEvent event ;
  183.     do
  184.         {
  185.         XNextEvent( recognizer.dpy(), &event ) ;
  186.         cout << "event type = " << event.type << "\n" << flush ;
  187.         if( recognizer.isSpeechEvent( &event ) )
  188.             recognizer.processEvent( &event ) ;
  189.          else
  190.             ; // process as other/normal event
  191.         } while( event.type != DestroyNotify ) ;    // just to get rid of warning
  192.  
  193.     XrmDestroyDatabase( xrmDatabase ) ;
  194.  
  195.     return recognizer.status( ) ;
  196.     }
  197.